package com.example.list;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ShortcutManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.example.utils.ToastUtil;

import java.lang.reflect.Method;
import java.security.Permission;

public class vivoShortcutPermissionActivity extends AppCompatActivity {

    private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 1;
    String readSettingsPermissions="com.bbk.launcher2.permission.READ_SETTINGS";

    //读写权限
    private static String[] PERMISSIONS_STORAGE1 = {
            "com.bbk.launcher2.permission.READ_SETTINGS",
            "com.bbk.launcher2.permission.WRITE_SETTINGS"};
    //读写权限
    private static String[] PERMISSIONS_STORAGE2 = {

            Manifest.permission.READ_PHONE_STATE
    };
    //请求状态码
    private static int REQUEST_PERMISSION_CODE = 1;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vivo_shortcut_permission);

        String[] PERMISSIONS_STORAGE = PERMISSIONS_STORAGE1;

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            if (ActivityCompat.checkSelfPermission(this, PERMISSIONS_STORAGE[0]) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this,  PERMISSIONS_STORAGE, REQUEST_PERMISSION_CODE);
            }
        }
//        Intent intent = new Intent();
//        intent.setClassName("com.bbk.launcher2", "com.bbk.launcher2.data.LauncherProvider");
//        startActivity(intent);

      int result=  getShortcut(this);
        Toast.makeText(this, "getShot:"+result, Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_PERMISSION_CODE) {
            for (int i = 0; i < permissions.length; i++) {
                Log.i("MainActivity", "申请的权限为：" + permissions[i] + ",申请结果：" + grantResults[i]);
//                Toast.makeText(this, "申请结果：" + grantResults[i], Toast.LENGTH_SHORT).show();
            }
        }
    }

//    int getShortcut(Context context) {
//        Cursor query = null;
//        try {
///*         E:\listdemo\DemoList>adb shell "dumpsys window | grep mCurrentFocus"
//           mCurrentFocus=Window{b3629ee u0 com.bbk.launcher2/com.bbk.launcher2.installshortcut.PurviewActivity
//           }*/
//
////            Class<?> shortcutList = Class.forName("com.bbk.launcher2.installshortcut.PurviewActivity");
//            Class<?> shortcutList = Class.forName("com.example.list.vivoShortcutPermissionActivity");
////            Class<?> shortcutList = context.getClassLoader().loadClass("com.vivo.settings.VivoSubSettings");
////            Class<?> shortcutList = Class.forName("com.vivo.permissionmanager.activity.SoftPermissionDetailActivity");
////            Class shortcutList = context.getClassLoader().loadClass("android.util.FtFeature");
//            Method contentResolverReflection = shortcutList.getDeclaredMethod("getContentResolver");
//            if (contentResolverReflection == null) {
//                return 2;
//            }
//            ContentResolver contentResolver = (ContentResolver) contentResolverReflection.invoke(shortcutList);
//            Uri parse = Uri.parse("content://com.bbk.launcher2.settings/favorites");
//            query = contentResolver.query(parse, null, null, null, null);
//            if (query == null) {
//                return 2;
//            }
//            while (query.moveToNext()) {
//                String titleByQueryLauncher = query.getString(query.getColumnIndexOrThrow("title"));
//                if (!TextUtils.isEmpty(titleByQueryLauncher)) {
//                    int value = query.getInt(query.getColumnIndexOrThrow("shortcutPermission"));
//                    if (value == 1 || value == 17) {
//                        return -1;
//                    } else if (value == 16) {
//                        return 0;
//                    } else if (value == 18) {
//                        return 1;
//                    }
//                }
//            }
//        } catch (Exception e) {
//            Log.d("权限exception111", "getShortcut: "+e.getMessage());
//            ToastUtil.showToast( e.getMessage());
//            Log.d("权限exception222", "getShortcut: "+e.getMessage());
//        } finally {
//            if (query != null) {
//                query.close();
//            }
//        }
//        return 2;
//    }

    int getShortcut(Context context) {
        Cursor query = null;
        try {
            ContentResolver contentResolver = context.getContentResolver();
            if (contentResolver == null) {
                return 2;
            }
            Uri parse = Uri.parse("content://com.bbk.launcher2.settings/favorites");
//            Uri parse = Uri.parse("content://com.bbk.launcher2.data.LauncherProvider/favorites");
//            Uri parse = Uri.parse("content://com.vivo.permissionmanager.provider.permission/read_installed_apps");//可以访问的uri资源
            query = contentResolver.query(parse, null, null, null, null);
            if (query == null) {
                Log.d("权限", "query == null ");
                return 2;
            }
            while (query.moveToNext()) {
                String titleByQueryLauncher = query.getString(query.getColumnIndexOrThrow("title"));
                if (!TextUtils.isEmpty(titleByQueryLauncher)) {
                    int value = query.getInt(query.getColumnIndexOrThrow("shortcutPermission"));
                    if (value == 1 || value == 17) {
                        return -1;
                    } else if (value == 16) {
                        return 0;
                    } else if (value == 18) {
                        return 1;
                    }
                }
            }
        } catch (Exception e) {
            Log.d("权限exception", "getShortcut: "+e.getMessage());
            ToastUtil.showToast(e.getMessage());
        } finally {
            if (query != null) {
                query.close();
            }
        }
        return 2;
    }
}